# -*- coding: utf-8 -*-
'''
@datetime : 2022/6/9 18:35
@author   : zhangxp
@file     : tasks.py
'''
from requests import request
from .celery import app

import sys
import os
import re
import time
import socket
from celery import group
from rqalpha import run_file, run_code

from copy import deepcopy
from pytz import timezone
from sqlalchemy import DATETIME, DATE, VARCHAR, create_engine
import numpy as np
import pandas as pd

from concurrent.futures import ThreadPoolExecutor,wait,ALL_COMPLETED

##########################################通用任务##################################################
def method_caller(mod, method_name, method_param=None):
    obj = __import__(mod, fromlist=True)
    if hasattr(obj, method_name):
        func = getattr(obj, method_name)
        if method_param is None:
            return func()
        else:
            return func(method_param)
    else:
        raise NameError("There is no attr '%s' " % method_name, "in %s.py" % mod)


@app.task
def task_io(mod, method_name, method_param=None):
    result = method_caller(mod, method_name, method_param=method_param)
    if isinstance(result, pd.DataFrame):
      return result.to_dict('list')
    return result